<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="it.cst.entity.service.*" %>
<%@ page import="it.cst.entity.*" %>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.servlet.support.RequestContextUtils" %>
<%@ page import= "java.util.HashMap" %>
<%@ page import= "it.cst.entity.controller.MatchController" %>


<%
   ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
   UserService us = (UserService) ctx.getBean("userService");
   HashMap<String, User> lmu = us.listLastMatchUser();
%>
<!DOCTYPE html> 
<html> 
	<head> 
	<title>CST</title> 
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.css" />
	<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.js"></script>
	<script>
	function addMatch(){
		redScore = document.getElementById("redScore").value;
		bluScore = document.getElementById("bluScore").value;
		if(redScore == bluScore){
			$("#scoreAlert1").popup("open");
			//alert('I punteggi delle squadre non possono essere uguali !');
			return;
		}
			
		if(redScore < 5 && bluScore < 5){
			$("#scoreAlert2").popup("open");
			//alert('Una delle squadre deve aver totalizzato il punteggio massimo !');
			return;
		}
		if(document.getElementById("userRedDefense").value != document.getElementById("userRedAttac").value &&
		   document.getElementById("userRedDefense").value != document.getElementById("userBluDefense").value &&
		   document.getElementById("userRedDefense").value != document.getElementById("userBluAttac").value &&
		   document.getElementById("userRedAttac").value != document.getElementById("userBluDefense").value &&
		   document.getElementById("userRedAttac").value != document.getElementById("userBluAttac").value &&
		   document.getElementById("userBluDefense").value != document.getElementById("userBluAttac").value)
		{
			document.getElementById("addMatchData").submit();
		}
		else
		{
			$("#playerAlert").popup("open");
			//alert('I giocatori devono essere tutti diversi !');
			return;
		}
	}
	</script>
</head> 
<body> 
<div data-role="page">

<div data-role="popup" id="scoreAlert1">
	<p>I punteggi delle squadre non possono essere uguali !<p>
</div>
<div data-role="popup" id="scoreAlert2">
	<p>Una delle squadre deve aver totalizzato il punteggio massimo !<p>
</div>
<div data-role="popup" id="playerAlert">
	<p>I giocatori devono essere tutti diversi !<p>
</div>
	<div data-role="header" style="padding:0;margin:0">
		<h1>Registra partita</h1>
	</div><!-- /header -->	
	<form name="addMatchDataForm" id="addMatchData" method="POST" action="insertMatchData" data-ajax="false"> 
		<div data-role="content" style="padding:0;margin:0">	
			<div data-role="fieldcontain" align="center" style="padding:0;margin:0">
			   <table style="width:100%;align:center;border-collapse:collapse">
			   	<tr bgcolor="red">
			   		<td style="width:50%">
			   			<font color="white">Difensore :</font>
			   		</td>
			   		<td style="width:50%">
					   	<select name="userRedDefense" id="userRedDefense" data-native-menu="false">
					      <%for(User user : us.listUser()){%>
					      	<%if(lmu != null && !lmu.isEmpty()){
					      		if(lmu.get(MatchController.USER_RED_DEFENSE).getUsername().equalsIgnoreCase(user.getUsername()))
					      		{
					      			%>
							      	<option selected value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
					      		}
					      		else
					      		{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
				      		    }
					      	  }
					      	  else{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%					      		
					      	  }
					       }%>
					    </select>
			   		</td>
			   	</tr>
			   	<tr bgcolor="red">
			   		<td style="width:50%">
			   			<font color="white">Attaccante :</font>
			   		</td>
			   		<td style="width:50%">
			   			<select name="userRedAttac" id="userRedAttac" data-native-menu="false">
					      <%for(User user : us.listUser()){%>
					      	<%if(lmu != null && !lmu.isEmpty()){
					      		if(lmu.get(MatchController.USER_RED_ATTAC).getUsername().equalsIgnoreCase(user.getUsername()))
					      		{
					      			%>
							      	<option selected value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
					      		}
					      		else
					      		{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
				      		    }
					      	  }
					      	  else{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%					      		
					      	  }					      	
					       }%>
					    </select>
			   		</td>
			   	</tr>
			   	<tr bgcolor="red">
			   		<td style="width:50%">
			   			<font color="white">Punteggio : </font>	
			   		</td>
			   		<td style="width:50%">
					   <select name="redScore" id="redScore" data-native-menu="false">
					   	  <option value="0">0</option>
					      <option value="1">1</option>
					      <option value="2">2</option>
					      <option value="3">3</option>
					      <option value="4">4</option>
					      <option value="5">5</option>
					    </select>
			   		</td>
			   	</tr>
			   </table>
			   <table style="width:100%;align:center;border-collapse:collapse">
			   	<tr bgcolor="blue">
			   		<td style="width:50%">
			   			<font color="white">Difensore :</font>
			   		</td>
			   		<td style="width:50%">
			   			<select name="userBluDefense" id="userBluDefense" data-native-menu="false">
					      <%for(User user : us.listUser()){%>
					      	<%if(lmu != null && !lmu.isEmpty()){
					      		if(lmu.get(MatchController.USER_BLU_DEFENSE).getUsername().equalsIgnoreCase(user.getUsername()))
					      		{
					      			%>
							      	<option selected value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
					      		}
					      		else
					      		{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
				      		    }
					      	  }
					      	  else{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%					      		
					      	  }					      	
					       }%>
					   </select>		    
			   		</td>
			   	</tr>
			   	<tr bgcolor="blue">
			   		<td style="width:50%">
			   			<font color="white">Attaccante :</font>
			   		</td>
			   		<td style="width:50%">
			   			<select name="userBluAttac" id="userBluAttac" data-native-menu="false">
					      <%for(User user : us.listUser()){%>
					      	<%if(lmu != null && !lmu.isEmpty()){
					      		if(lmu.get(MatchController.USER_BLU_ATTAC).getUsername().equalsIgnoreCase(user.getUsername()))
					      		{
					      			%>
							      	<option selected value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
					      		}
					      		else
					      		{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%
				      		    }
					      	  }
					      	  else{
					      			%>
							      	<option value="<%=user.getUsername()%>"><%=user.getFirstname()%> <%=user.getLastname()%></option>
							      	<%					      		
					      	  }
					       }%>
					    </select>
			   		</td>			   	
			   	</tr>
			   	<tr bgcolor="blue">
			   		<td style="width:50%">
			   			<font color="white">Punteggio :</font>
			   		</td>
			   		<td style="width:50%">
					   <select name="bluScore" id="bluScore" data-native-menu="false">
					      <option value="0">0</option>
					      <option value="1">1</option>
					      <option value="2">2</option>
					      <option value="3">3</option>
					      <option value="4">4</option>
					      <option value="5">5</option>
					    </select>
			   		</td>
			   	</tr>
			   </table>
			   <table style="width:100%;align:center;border-collapse:collapse">
			   	<tr>
			   		<td>
			   			<center><input type="button" value="Registra" onclick="addMatch()" data-ajax="false"/></center>
			   		</td>
			   	</tr>
			   	<tr>
			   		<td>
			   			<center><a href="home" data-role="button" data-ajax="false">Home</a></center>
			   		</td>
			   	</tr>
			   </table>
			</div>
		</div><!-- /content -->
	</form>
</div><!-- /page -->
</body>
</html>